package com.hna.eking.AirlineServer.Mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;

import com.hna.eking.AirlineServer.Mapper.MapInfo;

import org.apache.ibatis.annotations.SelectProvider;
import com.hna.eking.AirlineServer.Mapper.MapInfo;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
@Mapper
public interface MapInfoMapper {
    int insert(MapInfo record);

    int insertSelective(MapInfo record);

    @Select("SELECT * FROM t_map_info WHERE effect_flag = '0001' and map_code = #{0} ")
    List<MapInfo> selectByPrimaryKey(String mapCode);
    
    int updateByPrimaryKeySelective(MapInfo record);

    int updateByPrimaryKey(MapInfo record);

    
    @Select("SELECT * FROM t_map_info WHERE airport_code = #{0} and map_version > #{1}")
    List<MapInfo> selectByAirportCodeAndMapVersion(String airportCode, Integer mapVersion);
    
    @Select("SELECT * FROM t_map_info WHERE airport_code = #{0} and map_version > #{1} and effect_flag=#{2}")
    List<MapInfo> selectByAirportCodeAndMapVersionAndEffectFlag(String airportCode, Integer mapVersion, String effectFlag);
    
   //按机场和航图代码查询航图，仅查询有效的
    @Select("SELECT * FROM t_map_info WHERE  effect_flag='0001' and airport_code = #{0} and map_code = #{1}  ")
	MapInfo selectByAirportcodeAndMapcode(String airportCode, String mapCode);
    
    //按机场和航图代码查询航图，包括有效和无效的
    @Select("SELECT * FROM t_map_info WHERE airport_code = #{0} and map_code = #{1}  ")
	MapInfo selectByAirportcodeAndMapcode2(String airportCode, String mapCode);
	
    @Select("SELECT * FROM t_map_info WHERE  effect_flag='0001' and airport_code = #{1} ")
    List<MapInfo> selectListByAirportCode(String airportCode);
    
    @Select("SELECT count(*) FROM t_map_info WHERE airport_code = #{1} ")
   	int selectMaxMapCode(String airportCode);
    
	@Update("update t_map_info set map_name=#{mapName},"
			+ "map_type=#{mapType},file_path=#{filePath},map_version = (select max(map_version) + 1 from t_map_info where airport_code=#{airportCode}),"
			+ "update_date=#{updateDate},update_user=#{updateUser},effect_flag=#{effectFlag}"
			+ "where airport_code=#{airportCode} and map_code=#{mapCode}")
    public int UpdateMapInfo(MapInfo mapinfo) ;
	
	@Insert("INSERT INTO t_map_info(airport_code,map_code,map_name,map_type,file_path, map_version, "
			+ "create_date,create_user,update_date,update_user,effect_flag)"
			+ "VALUES(#{airportCode},#{mapCode},#{mapName},#{mapType},"
			+ "#{filePath},#{mapVersion},#{createDate},#{createUser},#{updateDate},"
			+ "#{updateUser},#{effectFlag})")
	public void InsertMapInfo(MapInfo mapinfo);
	
	@Update("update t_map_info set effect_flag = '0000', update_user = #{2},update_date = #{3},"
			+ "map_version = (select max(map_version) + 1 from t_map_info where airport_code=#{0}) "
			+ "where airport_code = #{0} and map_code = #{1}"  )
	public void DeleteMapInfo(String airportCode, String mapCode,String user, Date dt);
	
	@Update("update t_map_info set effect_flag = '0000', update_user = #{1},update_date = #{2},"
			+ "map_version = map_version + 1 "
			+ "where airport_code = #{0} "  )
	public void DeleteAllMapInfo(String airportCode, String user, Date dt);
	
	@SelectProvider(type = SqlUtils.class, method = "MapinfoSelectCountByInput")
	public int getCountByInput(String airportCode, String mapCode, String mapType);
	
	@SelectProvider(type = SqlUtils.class, method = "MapinfoSelectListByInput")
	public List<MapInfo> selectListByInput(String airportCode, String mapCode, String mapType, int limit, int offset);
	
	
	@Select("SELECT file_path FROM t_map_info WHERE airport_code = #{1} ")
	List<String> selectPathByAirportcode(String airportCode);
	

	@SelectProvider(type = SqlUtils.class, method = "MapinfoAreaCountByInput")
	public int MapinfoAreaCountByInput(String airportCode, String mapCode);
	
	@SelectProvider(type = SqlUtils.class, method = "MapinfoSelectAreaByInput")
	public List<MapInfo> MapinfoSelectAreaByInput(String airportCode, String mapCode,  int limit, int offset);
	
}